/*
 * Albareth - an action roleplaying game.
 * Copyright (C) 2008 Jens Anuth
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 *   02110-1301, USA.
 * 
 * $Id: GameEffect.java 45 2008-05-14 23:04:29Z jens464 $
*/
package albareth.core;

import albareth.core.entity.Entity;
import albareth.frontend.Tile;
import albareth.util.Location;

public class GameEffect
{
    public static final int STATIONARY = 0;
    public static final int ENTITY_BOUND = 1;
    public static final int CHASING = 2;
    public static final int FLYING = 3;
    public static final int PROJECTILE = 4;
    
    public static final int HIT_ENEMIES_ON_PATH = 0x2;
    public static final int HIT_ITEMS_ON_PATH = 0x4;
    public static final int FLY_INFINITE = 0x8;
    
    
    private GameEffectObserver _observer = null;
    private Entity _emitter;
    private int _type;
    private String _effectId;
    private Tile _tile;
    private boolean _spinning = false;
    long _duration = -1;
    float _speed = 0;
    Location _origin = null;
    Entity _entity = null;
    Entity _targetEntity = null;
    //GameEffect _explodeEffect = null;
    int _hitFlags = 0;
    Location _targetLocation = null;
    //the loc where this effect currently takes place:
    Location _currentLocation = null;
    private boolean _observerNotified = false;
    
    private GameEffect(int type, String effectId)
    {
        _type = type;
        _effectId = effectId;
    }

    private GameEffect(int type, Tile tile)
    {
        _type = type;
        _tile = tile;
    }

    public static GameEffect stationary(String effectId,
            Location origin, long duration)
    {
        GameEffect effect = new GameEffect(STATIONARY, effectId);
        effect.setOrigin(origin);
        effect.setDuration(duration);
        return effect;
    }
    
    public static GameEffect entityBound(String effectId, Entity entity,
            long duration)
    {
        GameEffect effect = new GameEffect(ENTITY_BOUND, effectId);
        effect.setEntity(entity);
        effect.setDuration(duration);
        return effect;
    }

    public static GameEffect chasing(String effectId, Location origin,
            Entity targetEntity, float speed, int hitFlags)
    {
        GameEffect effect = new GameEffect(CHASING, effectId);
        effect.setSpeed(speed);
        effect.setOrigin(origin);
        effect.setTargetEntity(targetEntity);
        effect.setHitFlags(hitFlags);
        return effect;
    }

    public static GameEffect flying(String effectId, Location origin,
            Location targetLocation, float speed, boolean spinning,
            int hitFlags)
    {
        GameEffect effect = new GameEffect(FLYING, effectId);
        effect.setSpeed(speed);
        effect.setOrigin(origin);
        effect.setTargetLocation(targetLocation);
        effect.setHitFlags(hitFlags);
        return effect;
    }
    
    public static GameEffect flyingProjectile(Tile tile, Location origin,
            Location targetLocation, float speed, boolean spinning,
            int hitFlags)
    {
        GameEffect effect = new GameEffect(PROJECTILE, tile);
        effect.setSpeed(speed);
        effect.setOrigin(origin);
        effect.setTargetLocation(targetLocation);
        effect.setHitFlags(hitFlags);
        effect.setSpinning(spinning);
        return effect;

    }
    
    public void setObserver(GameEffectObserver observer)
    {
        _observer = observer;
    }
    
    public final void notifyObserver()
    {
        if (_observer != null && !_observerNotified ) {
            _observerNotified = true;
            _observer.effectFinished(this);
        }
    }

    public int getType()
    {
        return _type;
    }

    public void setType(int type)
    {
        _type = type;
    }

    public String getEffectId()
    {
        return _effectId;
    }

    public void setEffectId(String effectId)
    {
        _effectId = effectId;
    }

    public long getDuration()
    {
        return _duration;
    }

    public void setDuration(long duration)
    {
        _duration = duration;
    }

    public Location getOrigin()
    {
        return _origin;
    }

    public void setOrigin(Location origin)
    {
        _origin = origin;
    }

    public Entity getEntity()
    {
        return _entity;
    }

    public void setEntity(Entity entity)
    {
        _entity = entity;
    }

    public Entity getTargetEntity()
    {
        return _targetEntity;
    }

    public void setTargetEntity(Entity targetEntity)
    {
        _targetEntity = targetEntity;
    }
    
    public void setHitFlags(int flags)
    {
        _hitFlags |= flags;
    }
    
    public boolean isHitFlagSet(int flags)
    {
        return (_hitFlags & flags) > 0;
    }

    public Location getTargetLocation()
    {
        return _targetLocation;
    }

    public void setTargetLocation(Location targetLocation)
    {
        _targetLocation = targetLocation;
    }

    public float getSpeed()
    {
        return _speed;
    }

    public void setSpeed(float speed)
    {
        _speed = speed;
    }

    public Location getCurrentLocation()
    {
        return _currentLocation;
    }

    public void setCurrentLocation(Location currentLocation)
    {
        _currentLocation = currentLocation;
    }

    public Tile getTile()
    {
        return _tile;
    }

    public void setTile(Tile tile)
    {
        _tile = tile;
    }

    public boolean isSpinning()
    {
        return _spinning;
    }

    public void setSpinning(boolean rotating)
    {
        _spinning = rotating;
    }

    public Entity getEmitter()
    {
        return _emitter;
    }

    public void setEmitter(Entity emitter)
    {
        _emitter = emitter;
    }


}
